<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Post;
use App\Models\AdminUser;
use App\Guards\AdminGuard;
use App\Events\UserScoreUpdateEvent;

class PostController extends Controller {

    public function __construct() {
        $this->middleware('need_admin_login');
    }

    public function posts($review=null) {
        $conditions = [];
        if($review) $conditions[] = ['status', '=' , 0];
        if(request()->admin_user->admin_type == AdminUser::LUO_ADMIN){
            $conditions[] = ['luo_id', '=', request()->admin_user->luo_id];
        }
        $posts = Post::where($conditions)->paginate(20);
        return view('admin.posts.list', ['posts' => $posts, 'review' => $review]);
    }

    public function review($id) {
        $post = Post::findOrFail($id);
        if(AdminGuard::can_admin_manage_post(request()->admin_user, $post)){
            $post->status = 1;
            $post->save();
            event(new UserScoreUpdateEvent($post->user, UserScoreUpdateEvent::USER_ADD_POST_EVENT));
        }
    }

    public function delete($id) {
        $post = Post::findOrFail($id);
        if(AdminGuard::can_admin_manage_post(request()->admin_user, $post)){
            $post->delete();
        }
    }
}
